﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Models;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Crypto;
using System.Security.Claims;
using System.Xml.Linq;

namespace DFGK_WMS.WebAPI.Controllers.基础数据
{
    /// <summary>
    /// 灾情等级管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DisasterLevelController : ControllerBase
    {
        private readonly ILogRepository logRepository;
        public IDisasterLevelRepository DisasterLevelService { get; set; }
        public DisasterLevelController(IDisasterLevelRepository disastlevelservice, ILogRepository logRepository)
        {
            DisasterLevelService = disastlevelservice;
            this.logRepository = logRepository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddDisasterLevel(DisasterLevelDto dto)
        {
            if (string.IsNullOrEmpty(dto.Name))
            {
                return Ok(BaseResult.Result("400", "名称不能为空！"));
            }
            bool isko = DisasterLevelService.AddDisasterLevel(dto);
            if (isko)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 4;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "创建了灾情等级" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败", "500"));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="material"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdateDisasterLevel(DisasterLevelDto dto)
        {
            if(dto.Id<= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dto.Name))
            {
                return Ok(BaseResult.Result("400", "名称不能为空！"));
            }
            bool isko = DisasterLevelService.UpdateDisasterLevel(dto);
            if (isko)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int uid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 6;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = uid;
                logg.Description = uname + "修改了灾情等级" + dto.Name;
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }

        /// <summary>
        /// 删除灾情管理
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DelDisasterLevel(int ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool isko = DisasterLevelService.DelDisasterLevel(ids);
            if (isko)
            {
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog();
                logg.LogType = 1;
                logg.ActionType = 5;
                logg.ModuleName = "基础数据";
                logg.UserName = uname;
                logg.RealName = runame;
                logg.UserId = rid;
                logg.Description = uname + "删除了灾情等级";
                //获取前端IP地址
                logg.Ipaddress = ip;

                logRepository.AddTLod(logg);
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 批量删除灾情管理
        /// </summary>
        /// <param name="idsList"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult DelDisasterLevelList(int[] idsList)
        {
            if (idsList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var ids in idsList)
            {
                bool isko = DisasterLevelService.DelDisasterLevel(ids);
                string uname = HttpContext.User.FindFirstValue("UserName");
                string runame = HttpContext.User.FindFirstValue("RealName");
                int rid = Convert.ToInt32(HttpContext.User.FindFirstValue("Id"));
                string? ip = HttpContext.Connection.RemoteIpAddress?.ToString();
                TLog logg = new TLog
                {
                    LogType = 1,
                    ActionType = 5,
                    ModuleName = "基础数据",
                    UserName = uname,
                    RealName = runame,
                    UserId = rid,
                    Description = isko ? uname + "删除了灾情等级id：" + ids : uname + "删除失败id:" + ids,
                    //获取前端IP地址
                    Ipaddress = ip,
                    Status= isko ? 1 : 0,
                };
                logRepository.AddTLod(logg);
                if (!isko)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 灾情等级分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDisasterlevels(string? name, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var level = DisasterLevelService.GetDisasterlevels(name, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(level, "查询成功", "200", totalCount));
        }

        /// <summary>
        /// 灾情物料明细分页查询
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetDisasterMaterials(int ids, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var material = DisasterLevelService.GetDisasterMaterials(ids, pageIndex, pageSize, out totalCount);
            return Ok(BaseResult.Result(material, "查询成功", "200", totalCount));
        }
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Getdisasterlevel(int ids)
        {
            if (ids <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var level = DisasterLevelService.Getdisasterlevel(ids);
            return Ok(BaseResult.Result(level, "查询成功", "200"));
        }

    }
}
